<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use App\Jobs\AddUserApplesJob;


class AutoPlusApple extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cc:auto-plus-apple';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {


         User::query()
            ->where('grade','vip')
            ->where('id','<',2262981)
            ->where('vip_expired_at','>',Carbon::now())
            ->chunk(100, function (Collection $users) {

            if ($users->isEmpty()) return false;

            /**
             * @var User $user
             */
            foreach ($users as $user) {
                if($user->is_stop){
                  continue;
                }
                $queue = app()->environment('local') ? 'plusAppleOne' : 'plusAppleOne';

            
                $res = dispatch(new AddUserApplesJob($user))->onQueue($queue);
                  // dd($res);

            }   

            return true;

        });
    }
}
